<template>
    <div class="index clearafter">
        <module-head></module-head>
        <div class="content flex">
            <div class="w220">
                <menu-list></menu-list>
            </div>
            <div class="w980 flex_full">
                <div class="flex flex_between">
                  <p class="flex flex_vcenter pdb20">
                    <i class="titleTag"></i>
                    <span class="fRedBig">产品与角色关系管理</span>
                  </p>
                    <button class="longBtn" @click="addrelation()">新增关系管理</button>
                    <div id="searchbyname">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-md-4 control-label">产品名称:</label>
                                <div class="col-md-8">
                                    <select class="form-control" v-model="proitem">
                                        <option v-for="(index,item) in prolists" value="{{item.code+'/'+item.name}}">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="servicepeoplelist bg">
                    <table class="table table-hover table-striped">
                        <thead>
                            <tr>
                                <th>产品编码</th>
                                <th>产品名称</th>
                                <th>角色编码</th>
                                <th>角色名称</th>
                                <th>是否默认</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-for="item in tablelists" track-by="$index">
                                <td>{{item.productCode}}</td>
                                <td>{{item.productName}}</td>
                                <td>{{item.roleId}}</td>
                                <td>{{item.roleName}}</td>
                                <td>{{item.isDefault==1?"是":"否"}}</td>
                                <td align="left"><span class="glyphicon glyphicon-pencil" @click="addrelation(item)"></span><span class="glyphicon glyphicon-trash" @click="delerole(item)"></span></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <!-- 新增修改 -->
        <div class="modal fade" id="addrelation" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">新增产品与角色关系</h4>
                    </div>
                    <form class="form-horizontal cb" role="form" v-on:submit.prevent="saveproduct">
                        <div class="modal-body">
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品名称:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="currentname" disabled />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">产品编码:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <input type="text" class="form-control" required v-model="savedata.productCode" disabled />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">角色名称:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <select class="form-control" required v-model="savedata.roleId">
                                        <option value="{{item.id}}" v-for="(index,item) in rolelists">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">角色编码:<span class="start">*</span></label>
                                <div class="col-md-8">
                                    <select class="form-control" required v-model="savedata.roleId" disabled>
                                        <option value="{{item.id}}" v-for="(index,item) in rolelists">{{item.id}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-md-3 control-label">强制更新:</label>
                                <div class="col-md-8">
                                    <div class="radio">
                                        <label class="checkbox-inline">
                                            <input type="radio" name="sex" value="1" v-model="savedata.isDefault">是
                                        </label>
                                        <label class="checkbox-inline">
                                            <input type="radio" name="sex" value="0" v-model="savedata.isDefault">否
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer savebtn">
                            <button type="submit" class="default ok center">确认</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- 删除 -->
        <div class="modal fade" id="myModal-dele" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body cbafter">
                        <p class="text-center">确认删除{{currentname}}的角色：<span class="red">{{currentrolename}}？</span></p>
                    </div>
                    <div class="modal-footer">
                        <div class="centerbutton">
                            <button @click="confirmdele" class="default cancel center">确认删除</button>
                            <button type="button" class="default greynobg center " data-dismiss="modal" aria-hidden="true">取消</button>
                        </div>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal -->
        </div>
    </div>
</template>
<script>
import 'assets/lib/bootstrap-3.3.2/css/bootstrap.min.css'
import 'assets/lib/bootstrap-3.3.2/js/bootstrap.min.js'
import 'assets/reset.css'
import Public from 'assets/public.js'
import moduleHead from 'components/module-head'
import menuList from 'components/menu-list'
export default {
    data() {
            return {
                tablelists: [], //版本列表
                prolists: [], //产品下拉列表
                savedata: {
                    "isDefault": "",
                    "productCode": "",
                    "relationId": 0,
                    "roleId": "",

                },
                proitem: "",
                currentname: "",
                currentcode: "",
                rolelists: [],
                currentrolename:"",
                currentrelationId:"",
            }
        },
        computed: {},
        components: {
            moduleHead, menuList
        },
        ready() {
            var that = this;
            //获取产品下拉框列表
            $.when(Public.commonajax("base.tenantProductsService", "queryProductTenantByPcode", "[null]"))
                .done(function(res) {
                    if (res.code == 200) {
                        if (that.prolists.length == 0) {
                            that.prolists = res.body;
                            that.proitem = res.body[0].code + '/' + res.body[0].name;

                        }
                        that.prolists = res.body;
                    }

                });
            //获取角色列表
            $.when(Public.commonajax("base.roleService", "queryRoleByNameOrCode", "['']"))
                .done(function(res) {
                    if (res.code == 200) {
                        that.rolelists = res.body;
                        console.log(that.rolelists);
                    }
                })
        },
        methods: {
            // 获取列表数据
            getlist(productcode) {
                    var that = this;
                    $.when(Public.commonajax("base.roleService", "queryRoleByProductCode", "['" + productcode + "']"))
                        .done(function(res) {
                            if (res.code == 200) {
                                that.tablelists = res.body;
                            }
                        })
                },

                // 新增弹框
                addrelation(item) {
                    if (item) {
                        this.savedata = {
                            "isDefault": item.isDefault,
                            "productCode": this.currentcode,
                            "relationId": item.relationId,
                            "roleId": item.roleId,

                        }
                    } else {
                        this.savedata = {
                            "isDefault": "",
                            "productCode": this.currentcode,
                            "relationId":0,
                            "roleId": "",

                        };
                    }

                    $("#addrelation").modal("show");

                },
                // 保存修改或新增
                saveproduct() {
                    var that = this;
                    $.when(Public.commonajax("base.roleService", "saveProductRoleRel", "[" + JSON.stringify(this.savedata) + "]"))
                        .done(function(res) {
                            if (res.code == 200) {
                                $("#addrelation").modal("hide");
                                Public.ajaxPrompt("保存成功");
                                that.getlist(that.currentcode)
                            }else{
                                Public.ajaxPrompt(res.msg);
                            }
                        })
                },
                 // 删除模块弹框
                delerole(item) {
                    var that = this;
                    $("#myModal-dele").modal("show");
                    this.currentrolename = item.roleName;
                    this.currentrelationId=item.relationId
                },
                //确认删除
                confirmdele() {
                    var that = this;
                    $.when(Public.commonajax("base.roleService", "delteProductRoleRel", "['" + this.currentrelationId + "']"))
                        .done(function(res) {
                            if (res.code == 200) {
                                Public.ajaxPrompt("删除成功");
                                $("#myModal-dele").modal("hide");
                                that.getlist(that.currentcode)
                            } else {
                                Public.ajaxPrompt(res.msg);
                            }

                        })
                },
        },
        events: {

        },
        watch: {
            'proitem': function(val, oldval) {
                var strarr = val.split("/");
                this.currentcode = strarr[0];
                this.currentname = strarr[1];
                this.getlist(this.currentcode)
            }
        },
}
</script>
<style scoped>
.servicepeoplelist {
    padding: 20px
}

.servicepeoplelist table th {
    text-align: center;
    line-height: 36px;
    height: 36px;
}

.servicepeoplelist table td {
    line-height: 30px;
    text-align: center;
}

.servicepeoplelist table tr td:nth-child(1) {
    width: 200px
}

.servicepeoplelist table tr td:nth-child(2) {
    width: 200px
}

.servicepeoplelist table tr td:nth-child(3) {
    width: 125px
}

.servicepeoplelist table tr td:nth-child(4) {
    width: 125px
}

.servicepeoplelist table tr td:nth-child(5) {
    width: 80px
}

#searchbyname {
    width: 400px;
    margin-right: 20px;
}

.centerbutton {
    width: 280px;
    margin: 0 auto;
}

.centerbutton button {
    margin-left: 5px;
    display: inline-block;
}

span.red {
    font-weight: bold;
    color: #f86448
}

.form-horizontal .checkbox-inline {
    padding-top: 4px
}

.servicepeoplelist table tr td .glyphicon {
    display: inline-block;
    width: 30px;
    height: 30px;
}

.servicepeoplelist table tr td .glyphicon:hover {
    color: #1dc499;
    cursor: pointer;
}
.form-horizontal .radio{
    padding-top:4px

}
</style>
